package v1

import (
	"github.com/gin-gonic/gin"
	"guns-go/model/request"
	"guns-go/model/response"
	"guns-go/service"
	"strconv"
)

// @Tags Role
// @Summary 角色分页查询
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysRoleParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysRole/page [get]
func RolePage(c *gin.Context) {

	var R request.SysRoleParam
	_ = c.ShouldBindQuery(&R)

	err, rolePage := service.RolePage(&R, c)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(rolePage, c)
}

// @Tags Role
// @Summary 角色新增
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysRoleParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysRole/add [post]
func RoleAdd(c *gin.Context) {

	var R request.SysRoleParam
	_ = c.ShouldBindJSON(&R)

	err := service.RoleAdd(&R)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}

// @Tags Role
// @Summary 角色修改
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysRoleParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysRole/edit [post]
func RoleEdit(c *gin.Context) {

	var R request.SysRoleParam
	_ = c.ShouldBindJSON(&R)

	err := service.RoleEdit(&R)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}

// @Tags Role
// @Summary 角色删除
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysRoleParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysRole/delete [post]
func RoleDelete(c *gin.Context) {

	var R request.SysRoleParam
	_ = c.ShouldBindJSON(&R)

	err := service.RoleDelete(&R)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}

// @Tags Role
// @Summary 角色分配菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysRoleParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysRole/grantMenu [post]
func RoleGrantMenu(c *gin.Context) {

	var R request.SysRoleParam
	_ = c.ShouldBindJSON(&R)

	err := service.RoleGrantMenu(&R)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}

// @Tags Role
// @Summary 角色分配数据
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysRoleParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysRole/grantData [post]
func RoleGrantData(c *gin.Context) {

	var R request.SysRoleParam
	_ = c.ShouldBindJSON(&R)

	err := service.RoleGrantData(&R, c)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}

// @Tags Role
// @Summary 角色已选菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysRoleParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysRole/ownMenu [get]
func RoleOwnMenu(c *gin.Context) {

	var R request.SysRoleParam
	_ = c.ShouldBindQuery(&R)

	err, info := service.RoleOwnMenu(&R)

	ids := make([]string, 0)

	if len(info) > 0 {
		for _, id := range info {
			ids = append(ids, strconv.Itoa(id))
		}
	}

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(ids, c)
}

// @Tags Role
// @Summary 角色已选数据
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysRoleParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysRole/ownData [get]
func RoleOwnData(c *gin.Context) {

	var R request.SysRoleParam
	_ = c.ShouldBindQuery(&R)

	err, info := service.RoleOwnData(&R)

	ids := make([]string, 0)

	if len(info) > 0 {
		for _, id := range info {
			ids = append(ids, strconv.Itoa(id))
		}
	}

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(ids, c)
}
